var params = JSON.parse(window.localStorage.users)
console.log(params)
var id = params.id;
var name = params.name
var access_token = params.access_token
document.title = name
var dataAll
//get数据
$.ajax({
    type: "POST",
    url: api('/stock-bill/get-back-list'),
    data: {
        access_token:access_token,
        id:id,
    },
    dataType: "json",
    success: function (res) {
        console.log(res)
        dataAll = res.data.all
        if(res.data.all==''){
            $('.container_back').hide()
        }else {
            backTo(res.data.all)
        }
        console.log((res.data.small_title))
        if(res.data.small == ''){
            $('.container_count').hide()
        }else {
            part(res.data.small)
        }
        var title = res.data.small_title
        if(title){
            $('.part_title').html(title)
        }else {
            $('.part_title').hide()
        }
    }
});
//带回产品页面渲染
function backTo(data){
    // console.log(data)
    var html = ''
    $.each(data,function(i,item){
        if(item.stock<0){
            item.stock = 0
        }
        html += `
        <li class="food-item" data-id="${item.item_id}" data-part="1">
        <div class="food-image">
            <img src="${item.img}" alt="">
            <img src="./icon导出/Group 2@2x.png" alt="">
        </div>
        <div class="food-typeFace">
        <div class="name">${item.name}</div>
        <div>应带回:<span class="show_num">${item.stock}</span></div>
        <input class="placeholder" type="tel" pattern="[0-9]*" placeholder='请输入带回数量' onfocus="enterHandle(this)" onblur="comeHandle(this)">
        </div>
        </li>
        `
        $('.container_back .num').text(i+1)
        $('.container_back .food-list').html(html)
    })
}
//全部带回input监听
var input_num
function enterHandle(item) {
    input_num = item.value
    if(item.value){
        item.value= ''
        item.placeholder ='请输入带回数量'
    }
}
function comeHandle(item){
    // console.log(input_num.parseInt())
    var num = parseInt(input_num.replace(/[^0-9]/ig,""))
    // console.log(num)
    if(item.value){
        item.value='当前:'+item.value+''
        item.style.border = '1px solid #f5a623'
    }else if(!num){
        item.placeholder ='请输入带回数量'
    }else {
        item.value='当前:' + num +''
    }
}
//部分带回产品页面渲染
function part(data){
    var html = ''
    $.each(data,function(i,item){
        // console.log(item.stock)
        // console.log(item)
        var num = ''
        if(item.stock<0){
            item.stock = 0
        }
        html += `
        <li class="food-item" data-id="${item.item_id}" data-part="0">
        <div class="food-image">
            <img src="${item.img}" alt="">
        </div>
        <div class="food-typeFace">
        <div class="name">${item.name}</div>
        <div>库存:<span class="show_num">${item.stock}</span></div>
        <input class="placeholder" type="tel" placeholder='请输入带回数量' onfocus="enteHandle(this)" onblur="comHandle(this)">
        </div>
        </li>
        `
        $('.container_count .num').text(i+1)
        $('.container_count .food-list').html(html)
    })
}
/**
 *部分产品 输入框变化*/
//光标进入时候的变化
var part_num
function enteHandle(item) {
    part_num = item.value
    if(item.value){
        item.value= ''
        item.placeholder ='请输入带回数量'
    }
}
//光标离开时候的变化
function comHandle(item){
    // console.log(input_num.parseInt())
    var part = parseInt(part_num.replace(/[^0-9]/ig,""))
    // console.log(num)
    if(item.value){
        item.value='当前:'+item.value+''
        item.style.border = '1px solid #f5a623'
    }else if(!part){
        item.placeholder ='请输入带回数量'
    }else {
        item.value='当前:' + part +''
    }
}
$('body').on('click',function (e) {

    console.log()
})
//点击获取页面数据传送到服务器
function seveHandle(){
    // var data = []
    console.log(dataAll)
    if (dataAll == ''){
        inputEach()
    }else {
        var count = 0,
            length = 0
        $('.container_back input').each(function (i,item) {
            "use strict"
            if(!item.value){
                count++
            }
            length = i+1
        })
        if(count == length){
            layer.open({
                content: '请输入全部带回的数量'
                ,skin: 'msg'
                ,time: 2 //2秒后自动关闭
            });
        }else {
            inputEach()
            // $('input').each(function (i,item) {
            //     console.log(item)
            //     var num
            //     if(!item.value){
            //         num = 0
            //     }else {
            //         num = item.value.split(':')[1]
            //     }
            //     var food = {}
            //     food.id = $(this).parents('.food-item').attr('data-id')
            //     food.show_num = $(this).parents('.food-typeFace').find('.show_num').text()
            //     food.name = $(this).parents('.food-typeFace').find('.name').text()
            //     // console.log(food.name)
            //     food.num = num
            //     food.all_back= $(this).parents('.food-item').attr('data-part')
            //     data.push(food)
            // })
            // var jsonData = JSON.stringify(data)
            // layer.open({
            //     content: '请确认带回产品是否核对正确'
            //     // '请确认上架产品数量是否正确'
            //     // '请核对所有产品库存' '重新核对''以核对准确'
            //     ,btn: ['确定提交', '再看一下']
            //     ,yes: function(index){
            //         console.log(index)
            //         foodPost(jsonData)
            //         layer.close(index);
            //     }
            // });
        }
    }
}
//post数据
function inputEach() {
    var data = []
    $('input').each(function (i,item) {
        console.log(item)
        var num
        if(!item.value){
            num = 0
        }else {
            num = item.value.split(':')[1]
        }
        var food = {}
        food.id = $(this).parents('.food-item').attr('data-id')
        food.show_num = $(this).parents('.food-typeFace').find('.show_num').text()
        food.name = $(this).parents('.food-typeFace').find('.name').text()
        // console.log(food.name)
        food.num = num
        food.all_back= $(this).parents('.food-item').attr('data-part')
        data.push(food)
    })
    var jsonData = JSON.stringify(data)
    layer.open({
        content: '请确认带回产品是否核对正确'
        // '请确认上架产品数量是否正确'
        // '请核对所有产品库存' '重新核对''以核对准确'
        ,btn: ['确定提交', '再看一下']
        ,yes: function(index){
            console.log(index)
            foodPost(jsonData)
            layer.close(index);
        }
    });
}
function foodPost(data) {
    console.log(data)
    $.ajax({
        type: "POST",
        url: api('/stock-bill/create'),
        data: {
            access_token:access_token,
            id:id,
            type:'3',
            items:data,
        },
        dataType: "json",
        success: function (res) {
            if(res.success){
                window.location.href = './pave.html?v='+ random +''
            }else {
                console.log(res)
                layer.open({
                    content: res.message
                    ,skin: 'msg'
                    ,time: 2 //2秒后自动关闭
                });
            }
        }
    });
}
